﻿using DbToDocument.Core;
using DbToDocument.Core.LiteDB;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace DbToDocument.WebApp.Controllers
{
    /// <summary>
    /// 数据库控制器
    /// </summary>
    public class DataBaseController : Controller
    {
        private readonly ILogger<DataBaseController> _logger;
        private readonly ILiteDBService _liteDBService;

        public DataBaseController(ILogger<DataBaseController> logger, ILiteDBService liteDBService)
        {
            _logger = logger;
            _liteDBService = liteDBService;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public JsonResult List()
        {
            _logger.LogInformation("加载数据库配置列表");
            var resultModel = new ResultModel<DbConConfig[]>();
            resultModel.data = _liteDBService.dbConConfigs().ToArray();
            return Json(resultModel);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="conConfig"></param>
        /// <returns></returns>
        public JsonResult Save(DbConConfig conConfig)
        { 
            return Json(_liteDBService.Save(conConfig));
        }
    }
}
